package com.codergd.gra.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import java.io.File;
import java.io.IOException;
import java.util.UUID;


@Component
public class QRCode {
    @Autowired
    private QCloudUtil qCloudUtil;
    /**
     *
     * @param text url
     * @param width
     * @param height
     * @param filePath 保存的文件未知
     * @throws WriterException
     * @throws IOException
     */
    public String generateQRCodeImage(String text, int width, int height)
            throws WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
//        Path path = FileSystems.getDefault().getPath(filePath);
        String path = "D:\\360downloads\\tempimg\\";
        //保存的文件名
        String dFileName = UUID.randomUUID()+".PNG";

        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", new File(path+dFileName).toPath());
        //使用腾讯云对象存储服务
        qCloudUtil.upLoadFile(qCloudUtil.initClient(),path+dFileName,"pay/"+dFileName);

        // 删除本地文件
        File img = new File(path+dFileName);
        if (img.delete()){
            System.out.println("文件已删除：" + dFileName);
        }else {
            System.out.println("删除失败");
        }

        return dFileName;
    }

//    public static void main(String[] args) {
//        try {
//            generateQRCodeImage("Hello World", 350, 350);
//        } catch (WriterException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
}
